﻿/*************************************************
Author: will
Date: 10/17/2012
Description:
Notes:
*************************************************/

#ifndef __CItemMgr_h__
#define __CItemMgr_h__

#include "CTHashObjectMgr.h"
#include "CItemConfig.h"
#include "CScrollConfig.h"
#include "CMaterialConfig.h"
#include "CCrystalConfig.h"
#include "CLevelItemConfig.h"
#include "CGiftConfig.h"

#include "CDropItem.h"
#include "CTRandomNum.h"
#include "CTAsyncCmd.h"

enum EN_ITEM_PRICE_TYPE
{
    ITEM_BUY_PRICE_SILVER = 1,  //银币买价
    ITEM_BUY_PRICE_GOLD   = 2,  //金币买价    
    ITEM_BUY_PRICE_CONTRI = 3,  //贡献度买价
    ITEM_SELL_PRICE  = 4,   //银币卖价
};

class CInstanceId;
class CUserStorage;
class CUserCard;
class CInstanceDropItemConfigData;
class CInstanceConfigData;
class CPlayerBaseInfo;
class CItemMgr
{
public:
    CItemMgr()
    {
    }

    static int CountSize()
    {
        return CMaterialConfig::CountSize()
            + CScrollConfig::CountSize()
            + CCrystalConfig::CountSize()
            + CJewlConfig::CountSize();
    }

    int Init(const char * sBuff);

    int LoadConfig(const char* szCfgPath);

    //增加物品掉落副本的配置
    int AddInstanceDropConfig(CInstanceConfigData & stInstanceConfig);

    //检查是否可以操作 (ITEM_OPER_FLAG),
    int CheckCanOper (unsigned char ucItemType, unsigned int uiItemId, unsigned char ucOperType, bool & bRetFlag);

    //背包配置
    int GetConfig(unsigned char ucItemType, unsigned int uiItemID, CItemConfigData& stConfigData);

    int GetIconName (unsigned char ucItemType, unsigned short usItemCfgID, CTSizeString<MAX_ICON_NAME_LEN> & stIconName);

    //获取掉落数据.(优先根据szKey掉落, szKey为空串时,根据itemid掉落.) (uiDropRate指定物品的掉落概率, iDropCount指定掉落数量)
    int GetDropItemData(unsigned int uiGID, unsigned char ucItemType, unsigned int uiItemID, const char* szKey,
                        unsigned int uiDropRate, int iDropCount, CDropItemData& stDropItemData);

    int GetDropItemDataByKey (unsigned char ucItemType, const char* szKey, int iDropCount, CDropItemData& stDropItemData);

    //获取等级相关物品
    int GetLevelItem (unsigned int uiGID, unsigned char ucItemType, unsigned short usItemId, int iDropCount, CDropItemData& stDropItemData);
    
    bool IsLevelItem (unsigned char ucItemType, unsigned short usItemId);

    //掉落物品接口。(如果bCheckGridEnough设置为false, 建议父命令调用GetDropLeftItem, 处理子命令未成功添加的物品)
    int DropItem(CTAsyncCmd* pstParent, unsigned int uiGID, CDropItem& stDropItem, bool bCheckGridEnough = true);

    //掉落物品接口。(尽量添加物品到背包, 如果背包满了,物品未添加完, 物品走奖励流程领取)
    int DropItemEx (CTAsyncCmd* pstParent, unsigned int uiGID, CDropItem& stDropItem);

    //掉落活动奖励的礼包.
    int DropBonus (int iModule, unsigned int uiGID, unsigned int uiGiftId, int iCount, bool bCheckMutex = false);

    //消耗金币\银币
    int SubGold (unsigned int uiGID, int iModule, int & iNowGold, int iToSubGold);
    int SubGold (CPlayerBaseInfo & stPlayerBaseInfo, int iModule, int iToSubGold);

    int SubSilver(CPlayerBaseInfo& stPlayerBaseInfo, int iToSubSilver, int iModule);

    //获取掉落剩余物品接口
    int GetDropLeftItem(CTLib::CTAsyncCmd* pstParent, CDropItem& stLeftItem);

    //卷轴
    int GetScrollConfig(unsigned short usScrollConfigID, CScrollConfigData& stConfigData);

    //礼包
    bool IsGiftValid(unsigned int uiGiftId);
    int GetGiftConfig(unsigned int uiGiftConfigID, CGiftConfigUnit & stConfigData);

    //材料
    int GetMaterialConfig(unsigned short usMaterialConfigID, CMaterialConfigData& stConfigData);

    //魔晶
    int GetCrystalConfig(unsigned short usCrystalConfigID, CCrystalConfigData& stConfigData);

    //宝石
    int GetJewelConfig(unsigned short usConfigID, CJewlConfigData& stConfigData);

    //宠物果实
    int GetFruitConfig(unsigned short usFruitId, CFruitUnitConfig & stConfigData);

    //礼包互斥类型
    int IsSameMutexType(unsigned short usGiftId1, unsigned short usGiftId2);

    //获取魔晶提升的属性
    int GetCrystalAttr (unsigned short usCrystalConfigID, CAttrPair & stAttr);

    int GetPotenAttrAdd (unsigned short usCrystalConfigID, CAttrPair & stAttr);

    int GetItemDropType (unsigned char ucItemType, unsigned int uiItemId, CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey);

    int GetItemDropInstance (unsigned char ucItemType, unsigned short usItemId, CDropInstanceConfig & stDropInstances);
    
    bool FindItemDropInstance(CDropInstanceConfig & stDropInstances, CInstanceId & stCurIns, CInstanceId & stDropId);

    //获取合成后的魔晶
    int GetCombinedCrystal (unsigned short usOriginCrystal, CCrystalConfigData& stConfigData);

    //获取物品的价格
	int GetItemPrice(unsigned char ucItemType, unsigned short usConfigID, unsigned short usCount, EN_ITEM_PRICE_TYPE enPriceType, int &iPrices);
    
    //判断掉落物品后,背包是否会满
    bool CheckBagFull(CUserStorage& stUserStorage, CDropItem& stDropItem);

    int DropCrystalFrag (unsigned short usCrystalId, CDropItemData & stDropItemData);    

    //获取礼包中物品
    int GetGiftItem(unsigned int uiGfitID, unsigned int uiGid, CDropItem & stGiftItem, bool bPreView = true);

    int GetScrollConfig(CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey,
        CScrollConfigData& stConfigData);
    int GetMaterialConfig(CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey,
        CMaterialConfigData& stConfigData);
    int GetJewelConfig(CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey,
        CJewlConfigData& stConfigData);
    int GetCrystalConfig(CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey,
        CCrystalConfigData& stConfigData);

    //自动出售物品
    int AutoSell(CDropItem& stDropItem);

    //物品掉落通知
    void NotifyDropItem(unsigned int uiGID, CDropItemData & stDropItemData);
    void NotifyDropItem(unsigned int uiGID, CDropItem& stDropItem);

private:    
    void AddInsGroupConfig(CInstanceDropItemConfigData & stInsDropConfig, CInstanceId & stInstId);
    void AddInsSubtypeConfig (unsigned char ucItemType, CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey, CInstanceId & stInstId);
    void AddInsItemConfig (unsigned char ucItemType, unsigned short usItemId, CInstanceId & stInstId);
    int GetGiftItem(unsigned int uiGid, CGiftDropItemConfig & stItemConfig, CDropItemData & stGiftItem);    
    int GetItemsInGift(unsigned int uiGid, unsigned int uiGfitID, CDropItem & stGiftItem);
public:
    CItemConfig   _stItemConfig;
    CScrollConfig   _stScrollConfig;
    CMaterialConfig _stMaterialConfig;
    CCrystalConfig  _stCrystalConfig;
    CLevelItemConfig _stLevelItemConfig;
    CJewlConfig       _stJewelConfig;   //宝石配置
    CGiftConfig   _stGiftConfig;
    CFruitConfig   _stFruitCfg;
    CSealConfigData  _stSealCfg;
    CDropPartnerConfig _stDropPartnerCfg;
};


#endif // __CItemMgr_h__

